package com.blacktech.dbu.core.response;

/**
 * 统一响应码枚举
 *
 * @author Yanyunsong
 */
public enum ResponseCode {

    // 通用状态码 (2xx 成功)
    SUCCESS(200, "操作成功"),
    CREATED(201, "创建成功"),
    ACCEPTED(202, "请求已接受，正在处理中"),
    NO_CONTENT(204, "删除成功"),

    // 客户端错误 (4xx)
    BAD_REQUEST(400, "请求参数错误"),
    UNAUTHORIZED(401, "未授权，请登录"),
    FORBIDDEN(403, "权限不足，拒绝访问"),
    NOT_FOUND(404, "请求的资源不存在"),
    METHOD_NOT_ALLOWED(405, "请求方法不被允许"),
    CONFLICT(409, "资源冲突"),
    VALIDATION_FAILED(422, "数据验证失败"),
    TOO_MANY_REQUESTS(429, "请求过于频繁，请稍后重试"),

    // 服务器错误 (5xx)
    INTERNAL_SERVER_ERROR(500, "服务器内部错误"),
    SERVICE_UNAVAILABLE(503, "服务暂不可用"),
    GATEWAY_TIMEOUT(504, "网关超时"),

    // 业务错误码 (1000-1999)
    BUSINESS_ERROR(1000, "业务处理失败"),

    // 用户管理错误码 (1001-1099)
    USER_NOT_FOUND(1001, "用户不存在"),
    USERNAME_ALREADY_EXISTS(1002, "用户名已存在"),
    PASSWORD_INCORRECT(1003, "密码错误"),
    USER_DISABLED(1004, "用户已被禁用"),
    USER_LOCKED(1005, "用户已被锁定"),
    INVALID_TOKEN(1006, "无效的令牌"),
    TOKEN_EXPIRED(1007, "令牌已过期"),
    TOKEN_REFRESH_FAILED(1008, "令牌刷新失败"),

    // 权限管理错误码 (1100-1199)
    PERMISSION_DENIED(1100, "权限不足"),
    ROLE_NOT_FOUND(1101, "角色不存在"),
    ASSIGN_PERMISSION_FAILED(1102, "权限分配失败"),
    REMOVE_PERMISSION_FAILED(1103, "权限移除失败"),
    PRODUCT_ACCESS_DENIED(1104, "无权限访问该产品"),

    // 产品管理错误码 (1200-1299)
    PRODUCT_NOT_FOUND(1200, "产品不存在"),
    PRODUCT_CODE_ALREADY_EXISTS(1201, "产品代号已存在"),
    PRODUCT_DELETE_FAILED(1202, "产品删除失败（存在关联数据）"),
    PRODUCT_UPDATE_FORBIDDEN(1203, "无权限修改该产品"),

    // 元数据管理错误码 (1300-1399)
    SCHEMA_NOT_FOUND(1300, "数据库模式不存在"),
    SCHEMA_ALREADY_EXISTS(1301, "数据库模式已存在"),
    TABLE_NOT_FOUND(1302, "数据表不存在"),
    TABLE_ALREADY_EXISTS(1303, "数据表已存在"),
    COLUMN_NOT_FOUND(1304, "字段不存在"),
    COLUMN_ALREADY_EXISTS(1305, "字段已存在"),
    CONSTRAINT_NOT_FOUND(1306, "约束不存在"),
    CONSTRAINT_ALREADY_EXISTS(1307, "约束已存在"),
    INDEX_NOT_FOUND(1308, "索引不存在"),
    INDEX_ALREADY_EXISTS(1309, "索引已存在"),

    // 字段库错误码 (1400-1499)
    FIELD_LIBRARY_NOT_FOUND(1400, "字段库记录不存在"),
    FIELD_ALREADY_EXISTS(1401, "字段已存在"),
    FIELD_MAPPING_NOT_FOUND(1402, "字段映射不存在"),
    FIELD_MAPPING_ALREADY_EXISTS(1403, "字段映射已存在"),
    FIELD_TYPE_NOT_SUPPORTED(1404, "不支持的字段类型"),
    FIELD_VALIDATION_FAILED(1405, "字段验证失败"),

    // 数据字典错误码 (1500-1599)
    DICTIONARY_NOT_FOUND(1500, "数据字典不存在"),
    DICTIONARY_CODE_ALREADY_EXISTS(1501, "字典编号已存在"),
    DICTIONARY_ITEM_NOT_FOUND(1502, "字典项不存在"),
    DICTIONARY_ITEM_CODE_ALREADY_EXISTS(1503, "字典项编号已存在"),

    // DDL生成错误码 (1600-1699)
    DDL_GENERATION_FAILED(1600, "DDL脚本生成失败"),
    UNSUPPORTED_DATABASE_TYPE(1601, "不支持的数据库类型"),
    DDL_TEMPLATE_NOT_FOUND(1602, "DDL模板不存在"),

    // 文件操作错误码 (1700-1799)
    FILE_UPLOAD_FAILED(1700, "文件上传失败"),
    FILE_DOWNLOAD_FAILED(1701, "文件下载失败"),
    FILE_FORMAT_NOT_SUPPORTED(1702, "不支持的文件格式"),
    FILE_SIZE_EXCEEDED(1703, "文件大小超出限制"),

    // 系统错误码 (9000-9999)
    SYSTEM_BUSY(9000, "系统繁忙，请稍后重试"),
    DATABASE_ERROR(9001, "数据库操作失败"),
    NETWORK_ERROR(9002, "网络连接异常"),
    CONFIGURATION_ERROR(9003, "系统配置错误"),
    DEPENDENCY_SERVICE_ERROR(9004, "依赖服务异常");

    private final Integer code;
    private final String message;

    ResponseCode(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    /**
     * 根据错误码查找对应的响应码枚举
     */
    public static ResponseCode getByCode(Integer code) {
        for (ResponseCode responseCode : values()) {
            if (responseCode.getCode().equals(code)) {
                return responseCode;
            }
        }
        return INTERNAL_SERVER_ERROR;
    }

    /**
     * 判断是否为成功响应码
     */
    public static boolean isSuccess(Integer code) {
        return code != null && code >= 200 && code < 300;
    }

    /**
     * 判断是否为客户端错误
     */
    public static boolean isClientError(Integer code) {
        return code != null && code >= 400 && code < 500;
    }

    /**
     * 判断是否为服务器错误
     */
    public static boolean isServerError(Integer code) {
        return code != null && code >= 500 && code < 600;
    }

    /**
     * 判断是否为业务错误
     */
    public static boolean isBusinessError(Integer code) {
        return code != null && code >= 1000 && code < 2000;
    }
}